package com.duobeiyun.gmstest.work

import android.content.Context
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.duobeiyun.generamessagesdk.ErrorInfo
import com.duobeiyun.generamessagesdk.ResultCallback
import com.duobeiyun.generamessagesdk.bean.LoginParams
import com.duobeiyun.generamessagesdk.utils.TokenUtils
import com.duobeiyun.gmstest.TestInstance
import com.duobeiyun.gmstest.log.LogDebug
import com.duobeiyun.gmstest.log.LogOuter
import com.duobeiyun.gmstest.work.sample.WorkAsyncMode
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

/**
 * 登录测试
 */
class WorkLoginTest(appContext: Context, workerParams: WorkerParameters) :
        WorkAsyncMode(appContext, workerParams) {

    var token: String? = null
    var timestamp: Long? = null

    override fun setNumber() {
        TAG = "WorkLoginTest"
        number = "1"
    }
    override fun realWork(continuation: CancellableContinuation<Result>) {

        var userId = TestInstance.userId
        timestamp = System.currentTimeMillis();
        token = TokenUtils.createToke(TestInstance.TEST_APPID, userId, timestamp!!, TestInstance.TEST_APPKEY);

        TestInstance.login(LoginParams(token!!, userId!!, timestamp!!),
                object : ResultCallback<Void> {
                    override fun onSuccess(responseInfo: Void?) {
                        LogOuter.print("number:$number \t result:${true} \t name:LoginTest ")
//                            LogOuter.print(String.format("number:$number\tresult:%b\t%5s",true,"name:LoginTest"))
                        continuation.resume(Result.success())
                    }

                    override fun onFailure(errorInfo: ErrorInfo) {
                        LogDebug.logE(errorInfo.toString())
                        LogOuter.print("number:$number \t result:${false} \t name:LoginTest ")
                        continuation.resume(Result.success())
                    }
                })
    }


}


